package zcj.stu;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@Configuration
public class WebConfig {
    @ControllerAdvice
    static class MyControllerAdvice implements ResponseBodyAdvice<Object> {
        @Override
        public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
            /*if( methodParameter.getMethodAnnotation(ResponseBody.class)!=null){
                return true;
            }
            if(AnnotationUtils.findAnnotation(methodParameter.getContainingClass(),RequestBody.class)!=null){
                return true;
            }
            return false;*/
            return true;
        }
        @Override
        public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
            if(o instanceof Result){
                return o;
            }
            return Result.ok(0);
        }
    }
    @Controller
    static class Controller1{
//        @ResponseStatus(HttpStatus.OK)
        public ModelAndView foo(User uSe){
            System.out.println("foo,.,");
            return null;
        }
        @ModelAttribute("bar")
        public String bar(){
            System.out.println("bar,.,");
            return "bar";
        }
        @ResponseBody
        public User testResponseBodyAdvice(){
            System.out.println("foo,.,");
            return new User("zs",18);
        }
    }
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Data
    static class User{
        private String name;
        private Integer age;

        public User() {
        }

        public User(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    }
    @JsonInclude(JsonInclude.Include.NON_NULL)
    static class Result{
         static  String ok(Object object){
             return "200";
         }
    }
}
